package mn.idax.exchange.otc.service.impl;

import mn.idax.exchange.common.bean.ResponseData;
import mn.idax.exchange.otc.domain.*;
import mn.idax.exchange.otc.entity.OTCAds;
import mn.idax.exchange.otc.entity.OTCCertificate;
import mn.idax.exchange.otc.enums.OTCCertificateStatusEnum;
import mn.idax.exchange.otc.enums.response.BusinessApplyResponseEnum;
import mn.idax.exchange.otc.enums.response.OrderResponseEnum;
import mn.idax.exchange.otc.enums.response.SystemStatusResponseEnum;
import mn.idax.exchange.otc.exception.IdaxException;
import mn.idax.exchange.otc.mapper.OTCAdsMapper;
import mn.idax.exchange.otc.mapper.OTCCertificateMapper;
import mn.idax.exchange.otc.service.OTCCertificateService;
import mn.idax.exchange.otc.util.NickNameUtils;
import mn.idax.exchange.otc.vo.PageRequestDTO;
import mn.idax.exchange.otc.vo.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @Author jension
 * @Date Created in 14:08 2020/12/1
 */
@Service
public class OTCCertificateServiceImpl implements OTCCertificateService {

    @Autowired
    OTCCertificateMapper otcCertificateMapper;
    @Autowired
    OTCAdsMapper otcAdsMapper;

    @Override
    public ResponseData applyUserCertificate(UserApplyCertificateRequest userApplyCertificateRequest) {
        // 查询广告
        OTCAds otcAds = otcAdsMapper.selectByPrimaryKey(userApplyCertificateRequest.getAdId());
        if(otcAds == null){
            return ResponseData.error(SystemStatusResponseEnum.QUERY_NOT_EXIST.getCode(), "");
        }
        if(otcAds.getPublisherid().equals(userApplyCertificateRequest.getUserId())){
            throw new IdaxException(OrderResponseEnum.ORDER_BUYBYSELF.getRespCode());
        }

        Integer approvalId = otcAds.getPublisherid();
        // 查询用户资格
        OTCCertificate otcCertificate = otcCertificateMapper.selectByApprovalIdUserIdStatus(approvalId, userApplyCertificateRequest.getUserId(), OTCCertificateStatusEnum.APPLYING.getState());
        if (otcCertificate != null) {
            return ResponseData.error(BusinessApplyResponseEnum.ALREADY_APPLY.getCode(), "");
        }

        String imgs = String.join(",", userApplyCertificateRequest.getImgs());
        otcCertificate = new OTCCertificate();
        otcCertificate.setUserId(userApplyCertificateRequest.getUserId());
        otcCertificate.setApprovalUserId(approvalId);
        otcCertificate.setImg(imgs);
        otcCertificate.setStatus(OTCCertificateStatusEnum.APPLYING.getState());
        otcCertificate.setCreateTime(new Date());
        otcCertificate.setUpdateTime(new Date());
        otcCertificateMapper.insert(otcCertificate);
        return ResponseData.success();
    }

    @Override
    public UserCertificateDTO getUserCertificate(GetUserCertificateRequest getUserCertificateRequest) {
        // 查询广告
        OTCAds otcAds = otcAdsMapper.selectByPrimaryKey(getUserCertificateRequest.getAdId());
        if(otcAds == null){
            throw new IdaxException(SystemStatusResponseEnum.QUERY_NOT_EXIST.getCode());
        }
        if(otcAds.getPublisherid().equals(getUserCertificateRequest.getUserId())){
            throw new IdaxException(OrderResponseEnum.ORDER_BUYBYSELF.getRespCode());
        }


        // 获取用户资格
        OTCCertificate otcCertificate = otcCertificateMapper.selectByApprovalIdUserId(otcAds.getPublisherid(), getUserCertificateRequest.getUserId());
        if(otcCertificate == null){
            return new UserCertificateDTO(OTCCertificateStatusEnum.NOT_APPLIED.getState(),"");
        }
        return new UserCertificateDTO(otcCertificate.getStatus(),otcCertificate.getRefuseReason());
    }

    @Override
    public PageVo<MyExamineListDTO> myExamineList(Integer businessId, PageRequestDTO pageRequestDTO) {
        List<MyExamineListDTO> myExamineListDTOS = otcCertificateMapper.selectByBusinessIdStatus(businessId, OTCCertificateStatusEnum.APPLYING.getState(),pageRequestDTO);
        Integer count = otcCertificateMapper.countByBusinessIdStatus(businessId, OTCCertificateStatusEnum.APPLYING.getState());

        if(count == null){
            count = 0;
        }
        for (MyExamineListDTO myExamineListDTO : myExamineListDTOS) {
            myExamineListDTO.setAccount(NickNameUtils.concatNickName(null, myExamineListDTO.getAccount()));
        }
        return  new PageVo(pageRequestDTO.getPageIndex() ,count, myExamineListDTOS);
    }

    @Override
    public ResponseData certificateExamine(CertificateExamineRequest certificateExamineRequest) {
        // 查询审核是否存在
        List<OTCCertificate> otcCertificates = otcCertificateMapper.selectByIdBusinessIdStatus(certificateExamineRequest.getCertificateId(), certificateExamineRequest.getUserId(), OTCCertificateStatusEnum.APPLYING.getState());
        if(otcCertificates == null || otcCertificates.isEmpty()){
            return ResponseData.error(SystemStatusResponseEnum.QUERY_NOT_EXIST.getCode(), "");
        }
        OTCCertificate updateObject = new OTCCertificate();
        updateObject.setUpdateTime(new Date());
        updateObject.setId(certificateExamineRequest.getCertificateId());
        updateObject.setStatus(certificateExamineRequest.getStatus());
        updateObject.setRefuseReason(certificateExamineRequest.getRemark());
        otcCertificateMapper.updateByPrimaryKeySelective(updateObject);

        return ResponseData.success();
    }

    @Override
    public GetCertificateDetailsDTO getCertificateDetails(GetCertificateDetailsRequest getCertificateDetailsRequest) {
        // 查询审核是否存在
        OTCCertificate otcCertificate = otcCertificateMapper.selectByIdUserIdOrBusinessId(getCertificateDetailsRequest.getCertificateId(), getCertificateDetailsRequest.getUserId());
        if(Objects.isNull(otcCertificate)){
            throw new IdaxException(SystemStatusResponseEnum.QUERY_NOT_EXIST.getCode());
        }

        return new GetCertificateDetailsDTO(otcCertificate.getImg().split(","));
    }
}
